unit svm_toy;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ShellAPI;

type
  TMainForm = class(TForm)
    MainPageControl: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    LoadGroupBox: TGroupBox;
    ScaleGroupBox: TGroupBox;
    MainStatusBar: TStatusBar;
    LoadTrainLabel: TLabel;
    LoadTrainEdit: TEdit;
    LoadTrainButton: TButton;
    ScaleTrainButton: TButton;
    TrainFileEdit: TEdit;
    Label1: TLabel;
    ScaledTrainFileEdit: TEdit;
    GridGroupBox: TGroupBox;
    GridLabel: TLabel;
    ScaledTrainFile: TEdit;
    GridButton: TButton;
    TrainGroupBox: TGroupBox;
    TrainLabel: TLabel;
    SVMTrainFileEdit: TEdit;
    TrainFileLabel: TLabel;
    ScaledTrainFileLabel: TLabel;
    c_Label: TLabel;
    C_Edit: TEdit;
    g_Label: TLabel;
    G_Edit: TEdit;
    TrainButton: TButton;
    LoadTestLabel: TLabel;
    LoadTestEdit: TEdit;
    LoadTestButton: TButton;
    ScaleTestButton: TButton;
    TestFileEdit: TEdit;
    Label2: TLabel;
    ScaledTestFileEdit: TEdit;
    TestFileLabel: TLabel;
    ScaledTestFileLabel: TLabel;
    PredictGroupBox: TGroupBox;
    TestLabel: TLabel;
    SVMTestFileEdit: TEdit;
    TestButton: TButton;
    OpenDialog1: TOpenDialog;
    OutPutMemo: TMemo;
    procedure LoadTrainButtonClick(Sender: TObject);
    procedure LoadTestButtonClick(Sender: TObject);
    procedure ScaleTrainButtonClick(Sender: TObject);
    procedure ScaleTestButtonClick(Sender: TObject);
    procedure GridButtonClick(Sender: TObject);
    procedure TrainButtonClick(Sender: TObject);
    procedure TestButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  TrainOfFile:TextFile;
  TestOfFile:TextFile;
  TrainFileName:string;
  TestFileName:string;
  ScaledTrainFileName:string;
  ScaledTestFileName:string;
  CurrentDir:string;

implementation

{$R *.dfm}

procedure TMainForm.LoadTrainButtonClick(Sender: TObject);
begin
  OpenDialog1.Filter := 'Text File';
  OpenDialog1.InitialDir := '.\data';
  if OpenDialog1.Execute then
  begin
    AssignFile(TrainOfFile,OpenDialog1.FileName);
    LoadTrainEdit.Text := OpenDialog1.FileName;
    TrainFileName := ExtractFileName(OpenDialog1.FileName);
    TrainFileEdit.Text := TrainFileName;
    ScaledTrainFileName := TrainFileName + '.scale';
    ScaledTrainFileEdit.Text := ScaledTrainFileName;
    ScaledTrainFile.Text := ScaledTrainFileName;
    SVMTrainFileEdit.Text := ScaledTrainFileName;
  end;

end;

procedure TMainForm.LoadTestButtonClick(Sender: TObject);
begin
  OpenDialog1.Filter := 'Text File';
  OpenDialog1.InitialDir := '.\data';
  if OpenDialog1.Execute then
  begin
    AssignFile(TestOfFile,OpenDialog1.FileName);
    LoadTestEdit.Text := OpenDialog1.FileName;
    TestFileName := ExtractFileName(OpenDialog1.FileName);
    TestFileEdit.Text := TestFileName;
    ScaledTestFileName := TestFileName + '.scale';
    ScaledTestFileEdit.Text := ScaledTestFileName;
    SVMTestFileEdit.Text := ScaledTestFileName;
  end;
end;

procedure TMainForm.ScaleTestButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
  ShellExecute(Handle,'open',PChar('cmd.exe'),
               PChar('/k svm-scale -r data\scale data\' + TestFileEdit.Text +
                     '>data\' + ScaledTestFileEdit.Text),nil,SW_HIDE);
end;

procedure TMainForm.ScaleTrainButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
  ShellExecute(Handle,'open',PChar('cmd.exe'),
               PChar('/k svm-scale -s data\scale data\' + TrainFileEdit.Text +
                     '>data\' + ScaledTrainFileEdit.Text),nil,SW_HIDE);
//    ShellExecute(Handle,'open',PChar('cmd.exe'),PChar('/k svm-scale -s data\scale data\a1a>data\a1a.scale'),nil,SW_SHOWNORMAL);
end;

procedure TMainForm.TestButtonClick(Sender: TObject);
begin
   SetCurrentDir(CurrentDir);
   ShellExecute(Handle,'open',PChar('cmd.exe'),
                PChar('/k svm-predict data\' + SVMTestFileEdit.Text + ' data\' +
                      ScaledTrainFileEdit.Text + '.model' + ' data\' +
                      SVMTestFileEdit.Text + '.out'),nil,SW_HIDE);
end;

procedure TMainForm.TrainButtonClick(Sender: TObject);
begin
   SetCurrentDir(CurrentDir);
   ShellExecute(Handle,'open',PChar('cmd.exe'),
                PChar('/k svm-train -c ' + C_Edit.Text + ' -g ' + G_Edit.Text +
                      ' data\' + SVMTrainFileEdit.Text),nil,SW_HIDE);
end;

procedure TMainForm.GridButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
  ShellExecute(Handle,'open',PChar('cmd.exe'),
               PChar('/k python grid.py data\' + ScaledTrainFile.Text),
               nil,SW_SHOWNORMAL);
end;

begin
  CurrentDir := GetCurrentDir;

end.
